
package Channel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GetChannels {
    
    	private static List<String> channels = new ArrayList<String>();
	private static String channel = "";
	
	public static List<String> getChannelsFromDataBase() {
         channels.removeAll(channels);
	 Connection conn = null;
         try
         {             
             String dbName = "MultiChat";
             String url = "jdbc:derby:" + dbName + ";create=true";
             Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance ();
             conn = DriverManager.getConnection(url);
             String createString = "SELECT * FROM Channel";
             PreparedStatement stmt = conn.prepareStatement(createString);
          
             ResultSet rs = stmt.executeQuery(); 
             while (rs.next()) {
                    String channel = rs.getString("channel");
                     channels.add(channel);
             }
         }
         catch (Exception e)
         {
             System.err.println ("Cannot connect to database server");
             e.printStackTrace();
         }
         finally
         {
             if (conn != null)
             {
                 try
                 {
                     conn.close ();
                 }
                 catch (Exception e) {
                	 System.out.println("Error with closing");
                 }
             }
         }

		
	return channels;
	}
	

}
